import NodeModel from "../models/NodeModel";

export default class NodeService {
  /**
   * 创建一个节点计算机
   * @param data 
   * @returns 
   */
  static async create(data = {}) {
    const result = await NodeModel.create(data)
    return result.toJSON()
  }

  /**
   * 删除
   * @param id 
   * @returns 
   */
  static async del(id: string) {
    const result = await NodeModel.destroy({
      where: {
        id
      }
    })

    return result > 0 ? true : false
  }

  /**
   * 修改
   * @param data 
   * @returns 
   */
  static async put(id = '', data = {}) {

    const result = await NodeModel.update({
      ...data
    }, {
      where: {
        // @ts-ignore
        id
      }
    })

    if (result[0] > 0) {
      return true
    } else {
      return false;
    }


  }

  /**
   * 分页查找
   * @param offset 
   * @param limit 
   * @param options 
   * @returns 
   */
  static async findByPage(offset: number = 0, limit: number = 10, options = {}) {
    const { rows, count } = await NodeModel.findAndCountAll({
      where: {
        ...options
      },
      offset,
      limit,
    })

    return {
      data: rows,
      total: count
    }
  }
}